package com.imooc.articles_mongodb_project.service.impl;

import com.imooc.articles_mongodb_project.common.Result;
import com.imooc.articles_mongodb_project.dao.ArticlesRepository;
import com.imooc.articles_mongodb_project.dto.ArticleDTO;
import com.imooc.articles_mongodb_project.entity.Articles;
import com.imooc.articles_mongodb_project.query.ArticleQuery;
import com.imooc.articles_mongodb_project.service.IArticlesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class ArticleServiceImpl implements IArticlesService {

    //注入dao
    @Autowired
    private ArticlesRepository articlesRepository;

    public List<Articles> getAll() {
        return articlesRepository.findAll();
    }

    public Result save(ArticleDTO articleDTO) {
        Articles articles = new Articles();
        articles.setTitle(articleDTO.getTitle()).setContent(articleDTO.getContent())
                .setAuthName(articleDTO.getAuthName()).setDate(new Date());
        articlesRepository.insert(articles);
        if (articles.getId() != null) {
            return new Result(200, "保存成功", articles.getId());
        } else {
            return new Result(400, "保存失败");
        }
    }


    public Result getById(String id) {
        Optional<Articles> articlesOptional = articlesRepository.findById(id);
        if (articlesOptional.isPresent()) {
            return new Result(200, "查询成功", articlesOptional.get());
        }
        return new Result(400, "查询失败");
    }

    public Result list() {
        return new Result(200, "查询成功", articlesRepository.findAll());
    }

    public Result listByParams(ArticleQuery articleQuery) {

        Articles articles = new Articles();
        if (articleQuery.getTitle() != null) {
            articles.setTitle(articleQuery.getTitle());
        }
        if (articleQuery.getAuthName() != null) {
            articles.setAuthName(articleQuery.getAuthName());
        }

        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);


        Example<Articles> example = Example.of(articles, exampleMatcher);
        List<Articles> articlesList = articlesRepository.findAll(example);
        return new Result(200, "查询成功", articlesList);
    }


    public Result update(ArticleDTO articleDTO) {
        Optional<Articles> articlesOptional = articlesRepository.findById(articleDTO.getId());
        if (!articlesOptional.isPresent()) {
            return new Result(400, "更新失败");
        }

        Articles articles = articlesOptional.get();
        articles.setTitle(articleDTO.getTitle()).setContent(articleDTO.getContent()).setAuthName(articleDTO.getAuthName());
        articlesRepository.save(articles);
        return new Result(200, "更新成功");
    }

    public Result deleteOne(String id) {
        articlesRepository.deleteById(id);
        Optional<Articles> articlesOptional = articlesRepository.findById(id);
        if (!articlesOptional.isPresent()) {
            return new Result(200, "删除成功");
        }
        return new Result(400, "删除失败");
    }
}































